package com.txsj.scan.presenter;

import com.google.gson.Gson;
import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.BaseResult;
import com.txsj.scan.bean.InventoryBatchBean;
import com.txsj.scan.bean.NewBatchBean;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.MD5Util;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.view.AddBatchIView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Created by WangChuangJun on 2018/11/21
 * @Describe
 */
public class AddBatchPresenter extends BasePresenter<AddBatchIView> {
    public AddBatchPresenter(AddBatchIView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }

    /**
     * 提交批次信息
     * @param batchBean
     */
    public void commitBatch(NewBatchBean batchBean,String warehouse_id) {
        Map<String,String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID,""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID,0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN,""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("data", new Gson().toJson(batchBean));
        params.put("warehouse_id", warehouse_id);
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        RetrofitManager.getDefault().create(ApiServer.class).commitBatchInfo(RetrofitManager.getHeader(),params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResult<InventoryBatchBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResult<InventoryBatchBean> result) {
                        if (result.isSuccess()){
                            if (null != result.getData()){
                                view.onSuccess(result.getData());
                            }else {
                                view.onError("提交失败，请重试");
                            }
                        }else {
                            view.onError("提交失败，请重试");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError("网络异常，请重试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
